/*
 * Yekcomputer (r) CMS
 * Copyright (c) 2009, Yekcomputer. All rights reserved.
 * Author : Babak Taraghi 'babak.taraghi@gmail.com'
 * Code licensed under the BSD License.
 * version: 1.0
 */
	 
var FormValidator={
	errors:new Array(),
	forms:new Array(),
	messages:new Array(),
	
	clear:function(){
		this.forms = new Array();
		this.errors = new Array();
		this.messages = new Array();
	},
	setElement:function(formId,id,persianName,type,isRequired){
		for(var index in this.forms){
			if(typeof this.forms[index] != "function" && typeof this.forms[index] != "boolean"){
				if(this.forms[index].id==formId){
					var formExist=true;
					form=this.forms[index];
				}else{
					var formExist=false;
				}
			}
		}
		if(!formExist){
			form=new Object();
			form.id=formId;
			form.elements=new Array();
			this.forms.push(form);
		}
		
		if(type=="radio"){
			i=0;
			while(ele=document.getElementById(id+(++i))){
				ele.ctype=type;
				ele.pName=persianName;
				ele.isR=isRequired;
				ele.mainId = id;
				form.elements.push(ele);
			}
		}else{
			ele=document.getElementById(id);
			ele.pName=persianName;
			ele.ctype=type;
			ele.isR=isRequired;
			form.elements.push(ele);
			ele=null;
		}
	},
	
	validate:function(formId){
		this.errors=new Array();
		this.radioError=false;
		this.radioChecked=false;
		for(var index in this.forms){
			if (typeof this.forms[index] != 'function' && typeof this.forms[index] != "undefined"){
				if(this.forms[index].id==formId){
					form=this.forms[index];
				}
			}
		}
		for(var index in form.elements){
			ele=form.elements[index];
			if (typeof ele.ctype != "undefined"){
				if((ele.ctype=="text"||ele.ctype=="email") && ele.isR && ele.value==""){
					if(typeof this.messages.text != "undefined")
						this.errors.push(eval(this.messages.text));
					else
						this.errors.push("Please fill out the "+ele.pName+" field.");
				}
				if(ele.ctype=="checkbox"&&ele.isR){
					if(!ele.checked){
						if(typeof this.messages.checkbox != "undefined")
							this.errors.push(eval(this.messages.checkbox));
						else
							this.errors.push("You have to check the "+ele.pName+" field.");
					}
				}
				if(ele.ctype=="radio" && ele.isR){
					if(ele.checked)
						this.radioChecked=ele.mainId;
					if(!ele.checked&&!this.radioError || this.radioError != ele.mainId){
						if(typeof this.messages.radio != "undefined")
							this.errors.push(eval(this.messages.radio));
						else
							this.errors.push("You need an option from "+ele.pName+"'s list.");
						this.radioError=ele.mainId;
					}
					if((this.radioChecked==ele.mainId||ele.checked)&&this.radioError == ele.mainId){
						this.errors.pop();
						this.radioError=false;
					}
				}
				if(ele.ctype==="select" && ele.isR && ele.value==""){
					if(typeof this.messages.select != "undefined")
						this.errors.push(eval(this.messages.select));
					else
						this.errors.push("Please select an option from "+ele.pName+"'s list.");
				}
				if(ele.ctype=="email" && ele.value!=""){
					p=ele.value.indexOf('@');
					if(p<1||p==(ele.value.length-1)){
						if(typeof this.messages.email != "undefined")
							this.errors.push(eval(this.messages.email));
						else
							this.errors.push("Entered email address in "+ele.pName+" field is not valid.");
					}
				}
				if(ele.ctype=="xinha"&&ele.isR&&xinha_editors[ele.id].getHTML()==""){
					if(typeof this.messages.xinha != "undefined")
						this.errors.push(eval(this.messages.xinha));
					else
						this.errors.push("You have to fill out the "+ele.pName+" field.");
				}
				if(ele.ctype.indexOf("-")!='-1'){
					splitedCtype=ele.ctype.split("-");
					if(splitedCtype[0]=="number"){
						if(ele.value!=""){
							val=parseFloat(ele.value);
							if(isNaN(val)){
								if(typeof this.messages.number != "undefined")
									this.errors.push(eval(this.messages.number));
								else
									this.errors.push("Please enter only numbers in "+ele.pName+" field.");
							}else if(splitedCtype[1]!=0){
								if(ele.value.length<splitedCtype[1]){
									if(typeof this.messages.numberLimit != "undefined")
										this.errors.push(eval(this.messages.numberLimit));
									else
										this.errors.push("Characters in "+ele.pName+" field is less than allowed number.");
								}
							}
						}else if(ele.isR&&ele.value==""){
							if(typeof this.messages.text != "undefined")
								this.errors.push(eval(this.messages.text));
							else
								this.errors.push("Please fill out the "+ele.pName+" field.");
						}
					}
				}
			}
		}
		if(this.errors.length>0){
			this.showErrors();
			return false;
		}
		return true;
	},
	
	
	showErrors:function(){
		if(typeof this.messages.errorsMessage != "undefined")
			errorsMessage = this.messages.errorsMessage;
		else
			errorsMessage = "Please click the next level button when you checked these: ";
		html = "<table width='100%' cellpadding='0' cellspacing='0' border='0'><tr height='1px'><td width='100%' dir='ltr'>"+errorsMessage+"<hr noshade='noshade'></td><td valign='top'><span style='cursor:pointer;' onclick='FormValidator.hideErrors()'>&nbsp;X</span></td></tr></table><div style='overflow:auto;width:350px;height:160px;'><table border='0' cellpadding='0' cellspacing='0' width='100%'>";
		if(document.getElementById("errorsContainer")){
			container=document.getElementById("errorsContainer");
			for(var index in this.errors){
				if (typeof this.errors[index] == 'string'){
					html+="<tr><td colspan='2' dir='ltr'>&bull;&nbsp;&nbsp;"+this.errors[index]+"</td></tr>";
				}
			}
			container.style.display="inline";
		}else{
			var container=document.createElement("div");
			container.setAttribute("id","errorsContainer");
			container.style.zIndex = "9999999";
			container.style.border = "1px solid black";
			container.style.position = (navigator.appName == "Microsoft Internet Explorer")?"absolute":"fixed";
			container.style.top = "200px";
			container.style.left = "280px";
			container.style.width = "360px";
			container.style.height = "200px";
			container.style.backgroundColor = "white";
			container.style.padding = "8px";
			container.style.boxShadow = "0 3px 5px #999999";
			
			for(var index in this.errors){
				if (typeof this.errors[index] == 'string'){
					html+="<tr><td colspan='2' dir='ltr'>&bull;&nbsp;&nbsp;"+this.errors[index]+"</td></tr>";
				}
			}
			document.body.appendChild(container);
		}
		html+="<tr height='100%'><td>&nbsp;</td></tr></table></div>";
		container.innerHTML=html;
		if(document.getElementById("content"))
			document.getElementById("content").style.opacity = ".12";
		for (var i in eles = document.getElementsByTagName("select")){
			if(eles[i].style && eles[i].style != "undefined"){
				eles[i].style.display="none";
			}
		}
	},
	
	hideErrors:function(){
		if(document.getElementById("errorsContainer")){
			document.getElementById("errorsContainer").style.display="none";
			for (var i in eles = document.getElementsByTagName("select")){
				if(eles[i].style && eles[i].style != "undefined"){
					eles[i].style.display="";
				}
			}
			if(document.getElementById("content"))
				document.getElementById("content").style.opacity = "1";
		}
	}
};